<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class News extends CI_Controller
{	
	var $num_per_page = 8;
  	var $num_links    = 3;
	
	public function __construct()
	{
		parent::__construct();

		$this->load->database();		
		$this->load->model('menu_model');
		$this->load->model('news_model');	
		$this->load->model('configuration_model');
	}

	/**
	 * Display login form.
	 */
	public function index($category_code = NULL , $code = NULL)
	{			
		//echo 'a';exit;
		$this->load->helper('news');
		$this->load->helper('paging');
		$this->load->library('BS_pagination', '', 'pagination');
		require_once APPPATH . 'modules/frontend/menu.php';	
		$this->view_data[]	= new stdClass();		
		$this->view_data['css_list'] = array(				
				base_url(). 'static/templates/frontend/home/css/typography.css',
				base_url(). 'static/templates/frontend/home/css/typo_tm.css'
		);
		
		$this->view_data['user_info']    = !empty( $_SESSION['user_info']) ?  $_SESSION['user_info'] : NULL;

		if(isset($code) && $code != ''){
			$news = $this->news_model->get_details($code);
                        $this->view_data['title'] = $news->title;
                        $this->view_data['content'] = $news->content;
                        
			if(!empty($news)){
				$this->news_model->update(array(
						'hits' => $news->hits + 1
				),$news->id);
			}
                        $typeCode           =   "news";

                        if ($category_code == "tin-tuc"){
                            $link           =   "/".$typeCode."/".$category_code;
                            $typeCode       =   "news";
                            $categoryTitle  =   "Tin tức";
                        }
                        if ($category_code == "su-kien"){
                            $link           =   "/".$typeCode."/".$category_code;
                            $typeCode       =   "event";
                            $categoryTitle  =   "Sự kiện";
                        }
                        if ($category_code == "huong-dan"){
                            $link           =   "/".$typeCode."/".$category_code;
                            $typeCode       =   "guide";
                            $categoryTitle  =   "Hướng dẫn";
                        }
                        $this->view_data['code']                =   $code;
                        $this->view_data['category_title']      =   $categoryTitle;
                        $this->view_data['link']                =   $link;
                        $this->view_data['list_news_rel']       =   $this->news_model->get_news($typeCode,$category_code,NULL,8,0,NULL);
			$this->view_data['child_view']          =   $this->load->view('frontend/news/details',$this->view_data,TRUE);
		}else{
			
			$limit = $this->num_per_page;
			$type = isset($category_code) ? $category_code : 'all';
			
			//$this->view_data['all_list']  	 = $this->news_model->get_news('news',NULL,NULL,$limit,$offset,array('tin-tuc','su-kien','huong-dan'));
			
                        if($type == "all"){
                                $this->view_data['all_list']  	 = $this->news_model->get_news('news',NULL,NULL,$limit,$offset,array('tin-tuc','su-kien','huong-dan'));
                                $config = paging(base_url()."news/get_contents/{$type}/",$this->news_model->get_rows('news',$category_code,NULL),$this->num_per_page,$this->num_links);
                                $this->pagination->initialize($config);
                                $this->view_data['all_paging']     = $this->pagination->create_links();
                                
                        } else {
                                $this->view_data['all_list'] = $this->news_model->get_news(NULL,$type,NULL,$limit,NULL,array('tin-tuc','su-kien','cong-dong','huong-dan'));
                                
                                if($type == "su-kien"){
                                    $subType = "event";
                                } else if($type == "huong-dan") {
                                    $subType = "guide";
                                } else {
                                    $subType = "news";
                                }
                                
                                $content = $this->news_model->_get_rows('news',$type,$subType);
                                $config = paging(base_url()."news/get_contents/{$type}/",$content[0]->count,$this->num_per_page,$this->num_links);
                                $this->pagination->initialize($config);
                                $this->view_data['all_paging']     = $this->pagination->create_links();

                        }
                        
                        $typeCode           =   "news";
                        $categoryTitle      =   "Tin tức";
                        $link               =   "";
                        if ($category_code == "su-kien"){
                            $link           =   "/".$typeCode."/".$category_code;
                            $typeCode       =   "event";
                            $categoryTitle  =   "Sự kiện";
                        }
                        if ($category_code == "huong-dan"){
                            $link           =   "/".$typeCode."/".$category_code;
                            $typeCode       =   "guide";
                            $categoryTitle  =   "Hướng dẫn";
                        }
                        
                        $this->view_data['category_title']      =   $categoryTitle;
                        $this->view_data['link']                =   $link;
			$this->view_data['child_view'] = $this->load->view('frontend/news/index',$this->view_data,TRUE);
		}  
		$this->load->view('templates/frontend/template',$this->view_data);
	}
	
	public function get_contents($type,$page=0){
		$this->load->helper('news');
		$this->load->helper('paging');
		$this->load->library('BS_pagination', '', 'pagination');
                $this->load->model('news_model');
                
		require_once APPPATH . 'modules/frontend/menu.php';	
		require_once APPPATH . 'modules/frontend/configuration.php';
		require_once APPPATH . 'modules/frontend/left.php';
                
		$this->view_data[]	= new stdClass();
		$this->view_data['css_list'] = array(				
				base_url(). 'static/templates/frontend/home/css/typography.css',
				base_url(). 'static/templates/frontend/home/css/typo_tm.css'
		);
                
		$this->view_data['user_info']    = !empty( $_SESSION['user_info']) ?  $_SESSION['user_info'] : NULL;
                $limit = $this->num_per_page;
                $offset = $page > 0 ? ($page - 1) * $this->num_per_page : $page;
                if($type == "all"){
                    $this->view_data['all_list']  	 = $this->news_model->get_news('news',NULL,NULL,$limit,$offset,array('tin-tuc','su-kien','huong-dan'));
                } else {
                    $this->view_data['all_list'] = $this->news_model->get_news(NULL,$type,NULL,$limit,$offset,array('tin-tuc','su-kien','cong-dong','huong-dan'));
                }

                if(empty($this->view_data['all_list'])){
                    redirect(base_url().'news/');
                }

                $config = paging(base_url()."news/get_contents/{$type}/",$this->news_model->get_rows('news',NULL,NULL),$limit);
                $this->pagination->initialize($config);
                $this->view_data['all_paging']     = $this->pagination->create_links();

                
                $typeCode           =   "news";
                $categoryTitle      =   "Tin tức";
                $link               =   "";
                if ($category_code == "su-kien"){
                    $link           =   "/".$typeCode."/".$category_code;
                    $typeCode       =   "event";
                    $categoryTitle  =   "Sự kiện";
                }
                if ($category_code == "huong-dan"){
                    $link           =   "/".$typeCode."/".$category_code;
                    $typeCode       =   "guide";
                    $categoryTitle  =   "Hướng dẫn";
                }

                $this->view_data['category_title']      =   $categoryTitle;
                $this->view_data['link']                =   $link;
                $this->view_data['child_view'] = $this->load->view('frontend/news/index',$this->view_data,TRUE);
                
                $this->load->view('templates/frontend/template',$this->view_data);
        }
}